{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# detect face\n",
    "import requests\n",
    "# json\n",
    "import json\n",
    "\n",
    "api_key=\"FxvLfOXTiiaAVP2xGYhz39quhwPHlB4u\" \n",
    "api_secret=\"KZ6cjKaeriPwtyz0wTT4uvSTnrBF-rpB\"\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 请求正文body\n",
    "img_url= \"https://gitee.com/edithx/face-detection/raw/master/yuqi.jpg\"\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 请求参数parameters\n",
    "data = {\n",
    "    'api_key':api_key,\n",
    "    'api_secret':api_secret,\n",
    "    \"image_url\":img_url,\n",
    "    'return_attributes':'gender,age,smiling,emotion',\n",
    "}\n",
    "\n",
    "r = requests.post(face_api_url, params=data,headers=headers)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603548888,9fddc596-8bd4-4bd2-8d84-495d333587a6\",\"time_used\":697,\"faces\":[{\"face_token\":\"86be76ec190bfab57ca044caeb8844a7\",\"face_rectangle\":{\"top\":100,\"left\":256,\"width\":127,\"height\":127},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":20},\"smile\":{\"value\":0.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.007,\"disgust\":0.007,\"fear\":0.112,\"happiness\":0.089,\"neutral\":98.950,\"sadness\":0.713,\"surprise\":0.122}}}],\"image_id\":\"zHo3BUsyJW1kAQBfOLF7fw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603548888,9fddc596-8bd4-4bd2-8d84-495d333587a6',\n",
       " 'time_used': 697,\n",
       " 'faces': [{'face_token': '86be76ec190bfab57ca044caeb8844a7',\n",
       "   'face_rectangle': {'top': 100, 'left': 256, 'width': 127, 'height': 127},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 0.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.007,\n",
       "     'disgust': 0.007,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 0.089,\n",
       "     'neutral': 98.95,\n",
       "     'sadness': 0.713,\n",
       "     'surprise': 0.122}}}],\n",
       " 'image_id': 'zHo3BUsyJW1kAQBfOLF7fw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '117900b822e80490789ac5b7e397cc92',\n",
       " 'time_used': 167,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603551082,7206a4bd-d6f4-49cd-97d4-c5fed3d8374f',\n",
       " 'outer_id': '19100',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"Gidle\"\n",
    "outer_id = \"19100\"\n",
    "user_data = \"50人，25男生，25女生\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}\n",
    "r = requests.post(CreateFace_Url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '117900b822e80490789ac5b7e397cc92',\n",
       " 'tags': '',\n",
       " 'time_used': 81,\n",
       " 'user_data': '50人，25男生，25女生',\n",
       " 'display_name': 'Gidle',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603551121,5e8ec806-964f-4c03-bc86-514862860288',\n",
       " 'outer_id': '19100'}"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "data = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':'19100',\n",
    "}\n",
    "r = requests.post(GetDetail_Url,params=data)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3、Add face\n",
    "Add_Face_Url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "params_add_face={\n",
    "    'api_key':api_key,\n",
    "    'api_secret':api_secret,\n",
    "    \"faceset_token\":'117900b822e80490789ac5b7e397cc92',\n",
    "    \"face_tokens\":'86be76ec190bfab57ca044caeb8844a7',\n",
    "}\n",
    "r_add_face = requests.post(Add_Face_Url,headers=headers,params=params_add_face)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'117900b822e80490789ac5b7e397cc92',\n",
    "    'face_tokens':'86be76ec190bfab57ca044caeb8844a7',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(RemoveFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '117900b822e80490789ac5b7e397cc92',\n",
       " 'face_removed': 0,\n",
       " 'time_used': 103,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603552238,b51db62a-1908-4934-a42f-568d57179d61',\n",
       " 'outer_id': '19100',\n",
       " 'failure_detail': [{'reason': 'FACE_NOT_IN_FACESET',\n",
       "   'face_token': '86be76ec190bfab57ca044caeb8844a7'}]}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '117900b822e80490789ac5b7e397cc92',\n",
       " 'request_id': '1603552295,69c8ef90-cf64-4b8f-9484-9178c24bfcbe',\n",
       " 'time_used': 62,\n",
       " 'outer_id': '19100'}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    \"faceset_token\":'117900b822e80490789ac5b7e397cc92',\n",
    "    'display_name':\"Gidle\",\n",
    "}\n",
    "r = requests.post(Update_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare Face（对比人脸相似度）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 223,\n",
       "    'top': 204,\n",
       "    'left': 215,\n",
       "    'height': 223},\n",
       "   'face_token': 'aa7e3a92949b8a91d6e7904367b172b7'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 554,\n",
       "    'top': 96,\n",
       "    'left': 569,\n",
       "    'height': 554},\n",
       "   'face_token': '67a3509f530765a4156bdb0e5bd6237a'}],\n",
       " 'time_used': 2752,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 52.606,\n",
       " 'image_id2': 'b1vxTc7vipkJSEnMdJCAMA==',\n",
       " 'image_id1': 'TZgL5G/otH8h1Nr+UsG1CA==',\n",
       " 'request_id': '1603552560,390f88ba-1229-4e32-9057-a42e52629387'}"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "全昭妍 = \"https://gitee.com/edithx/face-detection/raw/master/%E5%B0%8F%E7%8B%90%E7%8B%B8.jpg\"\n",
    "徐穗珍 = \"https://gitee.com/edithx/face-detection/raw/master/susu.jpg\"\n",
    "\n",
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':全昭妍,\n",
    "    'image_url2':徐穗珍,\n",
    "}\n",
    "r = requests.post(Compare_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
